feat(x402): add x402-protected chat endpoint at $0.01#178
feat(x402): add x402-protected chat endpoint at $0.01#178sweetmantech wants to merge 7 commits intotestfrom
Conversation
This commit adds a new x402-protected chat endpoint at /api/x402/chat with a price of $0.01 per request. All traffic to /api/chat now flows through the x402 endpoint using credit deductions and USDC payments. Changes: - Add CHAT_PRICE constant ($0.01) to lib/const.ts - Configure x402 middleware to protect POST /api/x402/chat - Create /api/x402/chat/route.ts endpoint handler - Add handleChatStreamX402 for x402-specific streaming - Add validateChatRequestX402 for x402 body validation (accountId required) - Add validateChatAuth for auth-only validation - Add fetchWithPaymentStream for POST requests with streaming - Add x402Chat wrapper function for calling x402 endpoint - Update /api/chat to route through x402 with credit deduction - Add comprehensive tests for all new functions Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the You can disable this status message by setting the
✨ Finishing touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Braintrust eval reportCatalog Opportunity Analysis Evaluation (HEAD-1769728888)
Catalog Songs Count Evaluation (HEAD-1769728888)
First Week Album Sales Evaluation (HEAD-1769728888)
Memory & Storage Tools Evaluation (HEAD-1769728888)
Monthly Listeners Tracking Evaluation (HEAD-1769728888)
Search Web Tool Evaluation (HEAD-1769728888)
Social Scraping Evaluation (HEAD-1769728888)
Spotify Followers Evaluation (HEAD-1769728888)
Spotify Tools Evaluation (HEAD-1769728888)
TikTok Analytics Questions Evaluation (HEAD-1769728888)
|
Extract the shared streaming logic into streamChatResponse function that both handleChatStream and handleChatStreamX402 can use. This eliminates code duplication and follows the KISS principle. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
Since /api/chat now routes through x402, handleChatStream is no longer used. Remove it and its tests to follow YAGNI principle. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
f168cb4 to
c67b6e1
Compare
The middleware matcher was running on all /api/* routes, which caused CORS preflight (OPTIONS) requests to /api/chat to be intercepted and redirected by the x402 middleware. Now the middleware only runs on /api/x402/* routes where x402 payment protection is needed. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
The x402 internal response already contains CORS headers, and we were adding our own on top, resulting in 'Access-Control-Allow-Origin: *, *'. Now we filter out any access-control-* headers from the internal response before adding our own CORS headers. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
loadAccount was hardcoded to use IMAGE_GENERATE_PRICE ($0.15) for all transfers. Now it accepts a price parameter so chat uses CHAT_PRICE ($0.01) and image generation uses IMAGE_GENERATE_PRICE ($0.15). Changes: - Update loadAccount to accept price parameter - Update fetchWithPayment to pass IMAGE_GENERATE_PRICE - Update fetchWithPaymentStream to pass the price parameter - Add tests for loadAccount - Update fetchWithPaymentStream tests Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
When streaming responses go through proxy middleware, they can be buffered/compressed causing glitchy streaming. Adding Content-Encoding: none header prevents this. See: https://ai-sdk.dev/docs/troubleshooting/streaming-not-working-when-proxied Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
recoup-coding-agent
left a comment
There was a problem hiding this comment.
Code Review: feat(x402): add x402-protected chat endpoint at $0.01
Summary
Adds an x402-paywall-protected chat endpoint that requires a $0.01 crypto payment (Base network) before each chat completion.
Issues Found
🔴 Merge Conflict
This PR is not currently mergeable — it has a merge conflict with test. This is a very old PR (178 in the sequence) and the codebase has changed significantly since it was opened. A full rebase would be needed.
🟡 Suggestions (pending rebase)
Once rebased, the following should be addressed:
-
Check
x402-nextversion compatibility — The project updated Next.js to 16.2.1 (PRs #324/#325). Verifyx402-nextis compatible with the new Next.js version. -
Payment amount hardcoded —
$0.01per request is hardcoded. Consider making it configurable via environment variable for easy tuning without code changes.
Verdict: needs-rebase
Interesting feature but needs a full rebase to be reviewable. The merge conflict makes it impossible to assess correctness against the current codebase.
Summary
/api/x402/chatwith $0.01 price/api/chattraffic through x402 endpoint with credit deductionsTest plan
/api/x402/chatendpoint works with x402 payment/api/chatproperly routes through x402 with credit deduction🤖 Generated with Claude Code